package domain

import (
	"AcsGo/internal/util"
	"fmt"
)

type BookRenewalRequest struct {
	Code             util.Field
	Fill             util.Field
	TransactionDate  util.Field
	NbDueDate        util.Field
	InstitutionId    util.FieldPrefix
	PatronIdentifier util.FieldPrefix
	ItemIdentifier   util.FieldPrefix
	ReaderLibid      util.FieldPrefix
	BarcodeLibid     util.FieldPrefix
}

// 29YN20240623    17272620240623    172726AO|AACSSF00100|AB130243|AC|CNsfsxbjjhj

func HandleBookRenewalRequest(str string) (*BookRenewalRequest, error) {
	if str == "" {
		return nil, fmt.Errorf("str is empty")
	}
	var query = BookRenewalRequest{
		Code:             util.Field{Value: "29", Length: 2},
		Fill:             util.Field{Value: "YN", Length: 2},
		TransactionDate:  util.Field{Value: "", Length: 18},
		NbDueDate:        util.Field{Value: "", Length: 18},
		InstitutionId:    util.FieldPrefix{Value: "", Prefix: "AO", Suffix: util.PrefixLink},
		PatronIdentifier: util.FieldPrefix{Value: "", Prefix: "AA", Suffix: util.PrefixLink},
		ItemIdentifier:   util.FieldPrefix{Value: "", Prefix: "AB", Suffix: util.PrefixLink},
		ReaderLibid:      util.FieldPrefix{Value: "", Prefix: "AC", Suffix: util.PrefixLink},
		BarcodeLibid:     util.FieldPrefix{Value: "", Prefix: "CN", Suffix: util.PrefixLink},
	}

	length := query.Code.Length + query.Fill.Length + query.TransactionDate.Length
	if len(str) < length {
		return nil, fmt.Errorf("str is too short")
	}

	strTemp := query.Code.GetValue(str)
	strTemp = query.Fill.GetValue(strTemp)
	strTemp = query.TransactionDate.GetValue(strTemp)
	strTemp = query.NbDueDate.GetValue(strTemp)
	strTemp = query.InstitutionId.GetValue(strTemp)
	strTemp = query.PatronIdentifier.GetValue(strTemp)
	strTemp = query.ItemIdentifier.GetValue(strTemp)
	strTemp = query.ReaderLibid.GetValue(strTemp)
	_ = query.BarcodeLibid.GetValue(strTemp)
	return &query, nil
}

type BookRenewalResponse struct {
	Code             util.Field
	OK               util.Field
	Fill             util.Field
	TransactionDate  util.Field
	InstitutionId    util.FieldPrefix
	PatronIdentifier util.FieldPrefix
	ItemIdentifier   util.FieldPrefix
	TitleIdentifier  util.FieldPrefix
	DueDate          util.FieldPrefix
	Loandate         util.FieldPrefix
	FeeType          util.FieldPrefix
	SecurityInhibit  util.FieldPrefix
	CurrencyType     util.FieldPrefix
	FeeAmount        util.FieldPrefix
	MediaType        util.FieldPrefix
	ItemProperties   util.FieldPrefix
	TransactionId    util.FieldPrefix
	ScreenMessage    util.FieldPrefix
	PrintLine        util.FieldPrefix
	SequenceNumber   util.FieldPrefix
}

func (q BookRenewalResponse) StringFormat() (string, error) {
	sb, err := util.ConcatenateFields(q)
	if err != nil {
		return "", err
	}
	return sb, nil
}

func InitBookRenewalResponse(success, rdbm, bkbm, dueDate, loadDate, bkTitle string) *BookRenewalResponse {
	var queryResponse = BookRenewalResponse{
		// |AF图书[130243]续借处理成功,还书日期为2024-07-24 ]] |AG图书[130243]续借处理成功,还书日期为2024-07-24 ]] |AY3AZD2A1
		Code:             util.Field{Value: "30", Length: 2},
		OK:               util.Field{Value: "1", Length: 1},
		Fill:             util.Field{Value: "YNN", Length: 3},
		TransactionDate:  util.Field{Value: util.GetCurrentTimeInFormat(), Length: 18},
		InstitutionId:    util.FieldPrefix{Value: "001", Prefix: "AO", Suffix: util.PrefixLink},
		PatronIdentifier: util.FieldPrefix{Value: "", Prefix: "AA", Suffix: util.PrefixLink},
		ItemIdentifier:   util.FieldPrefix{Value: "", Prefix: "AB", Suffix: util.PrefixLink},
		TitleIdentifier:  util.FieldPrefix{Value: "", Prefix: "AJ", Suffix: util.PrefixLink},
		DueDate:          util.FieldPrefix{Value: "", Prefix: "AH", Suffix: util.PrefixLink},
		Loandate:         util.FieldPrefix{Value: "", Prefix: "CM", Suffix: util.PrefixLink},
		FeeType:          util.FieldPrefix{Value: "", Prefix: "BT", Suffix: util.PrefixLink},
		SecurityInhibit:  util.FieldPrefix{Value: "001", Prefix: "CI", Suffix: util.PrefixLink},
		CurrencyType:     util.FieldPrefix{Value: "", Prefix: "BH", Suffix: util.PrefixLink},
		FeeAmount:        util.FieldPrefix{Value: "", Prefix: "BV", Suffix: util.PrefixLink},
		MediaType:        util.FieldPrefix{Value: "", Prefix: "CK", Suffix: util.PrefixLink},
		ItemProperties:   util.FieldPrefix{Value: "", Prefix: "CH", Suffix: util.PrefixLink},
		TransactionId:    util.FieldPrefix{Value: "", Prefix: "BK", Suffix: util.PrefixLink},
		ScreenMessage:    util.FieldPrefix{Value: "", Prefix: "AF", Suffix: util.PrefixLink},
		PrintLine:        util.FieldPrefix{Value: "", Prefix: "AG", Suffix: util.PrefixLink},
		SequenceNumber:   util.FieldPrefix{Value: "3AZD2A1", Prefix: "AY", Suffix: util.PrefixLink},
	}
	queryResponse.OK.Value = success
	queryResponse.PatronIdentifier.Value = rdbm
	queryResponse.ItemIdentifier.Value = bkbm
	queryResponse.TitleIdentifier.Value = bkTitle
	queryResponse.DueDate.Value = dueDate
	queryResponse.Loandate.Value = loadDate

	if success == "1" {
		queryResponse.ScreenMessage.Value = fmt.Sprintf("图书[%s]续借处理成功,还书日期为%s", bkbm, dueDate)
		queryResponse.PrintLine.Value = fmt.Sprintf("图书[%s]续借处理成功,还书日期为%s", bkbm, dueDate)
	} else {
		queryResponse.ScreenMessage.Value = fmt.Sprintf("图书[%s]续借处理失败 请联系管理员", bkbm)
		queryResponse.PrintLine.Value = fmt.Sprintf("图书[%s]续借处理失败请联系管理员", bkbm)
	}
	return &queryResponse
}
